<?php

namespace App\Services;

use App\Models\Crm_Crowd;
use App\Models\Crm_Crowd_Apply;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Throwable;

class CrowdService
{
    public function getCrowdList($params)
    {
        $result = Crm_Crowd::getFilter($params)
            ->Nodel()
            ->paginate($params['pageSize']);
        foreach ($result as $key => $value){
            $value->marry = json_decode($value->marry,true);
            $value->education = json_decode($value->education,true);
            $value->house = json_decode($value->house,true);
            $value->car = json_decode($value->car,true);
        }
        $list['total'] = $result->total();
        $list['list'] = $result->items();
        return $list;
    }

    public function crowdUpdate($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();
            if(!empty($params['id'])){
                $res = Crm_Crowd::where('id',$params['id'])->update([
                    'agencyid' => $params['aid'],
                    'name' => $params['name'],
                    'cover' => $params['cover'],
                    'type' => $params['type'],
                    'price' => $params['price'],
                    'desc' => $params['desc'],
                    'order' => $params['order'],
                    'backimg' => $params['backimg'],
                    'isopen' => $params['isopen'],
                    'gender' => $params['gender'],
                    'isreal' => $params['isreal'],
                    'province' => $params['province'],
                    'province_name' => $params['province_name'],
                    'city' => $params['city'],
                    'city_name' => $params['city_name'],
                    'area' => $params['area'],
                    'area_name' => $params['area_name'],
                    'reside_province' => $params['reside_province'],
                    'reside_province_name' => $params['reside_province_name'],
                    'reside_city' => $params['reside_city'],
                    'reside_city_name' => $params['reside_city_name'],
                    'reside_area' => $params['reside_area'],
                    'reside_area_name' => $params['reside_area_name'],
                    'minage' => $params['minage'],
                    'maxage' => $params['maxage'],
                    'marry' => $params['marry'],
                    'education' => $params['education'],
                    'house' => $params['house'],
                    'car' => $params['car']

                ]);
                if(!$res){
                    throw new \Exception("更新Crm_Crowd搭子失败" );
                }
            }else{
                $res = Crm_Crowd::create([
                    'agencyid' => $params['aid'],
                    'name' => $params['name'],
                    'cover' => $params['cover'],
                    'type' => $params['type'],
                    'price' => $params['price'],
                    'desc' => $params['desc'],
                    'order' => $params['order'],
                    'backimg' => $params['backimg'],
                    'isopen' => $params['isopen'],
                    'gender' => $params['gender'],
                    'isreal' => $params['isreal'],
                    'province' => $params['province'],
                    'province_name' => $params['province_name'],
                    'city' => $params['city'],
                    'city_name' => $params['city_name'],
                    'area' => $params['area'],
                    'area_name' => $params['area_name'],
                    'reside_province' => $params['reside_province'],
                    'reside_province_name' => $params['reside_province_name'],
                    'reside_city' => $params['reside_city'],
                    'reside_city_name' => $params['reside_city_name'],
                    'reside_area' => $params['reside_area'],
                    'reside_area_name' => $params['reside_area_name'],
                    'minage' => $params['minage'],
                    'maxage' => $params['maxage'],
                    'marry' => $params['marry'],
                    'education' => $params['education'],
                    'house' => $params['house'],
                    'car' => $params['car']
                ]);
                if(!$res){
                    throw new \Exception("新增Crm_Crowd搭子失败" );
                }
            }

            DB::commit();

            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function crowdDel($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();
            $res = Crm_Crowd::find($params['id']);
            $res->is_del = 'Y';

            if(!$res->save()){
                throw new \Exception("删除Crm_Crowd搭子失败" );
            }
            DB::commit();

            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }

    public function crowdIsopenUpdate($params)
    {
        try {
            // 开始数据库事务
            DB::beginTransaction();
            $res = Crm_Crowd::find($params['id']);
            $res->isopen = $params['isopen'];

            if(!$res->save()){
                throw new \Exception("更新Crm_Crowd搭子状态失败" );
            }
            DB::commit();

            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }
    public function getCrowdApplyList($params)
    {
        $result = Crm_Crowd_Apply::getFilter($params)->paginate($params['pageSize']);
        $result->data = $result->makeHidden(['hasOneCrowd']);
        $list['total'] = $result->total();
        $list['list'] = $result->items();
        return $list;
    }

    public function crowdApplyStatusUpdate($params)
    {
        $ids = explode(',',$params['ids']);
        try {
            // 开始数据库事务
            DB::beginTransaction();
            foreach ($ids as $id) {
                $res = Crm_Crowd_Apply::find($id);
                $res->status = $params['status'];

                if(!$res->save()){
                    throw new \Exception("更新Crm_Crowd_Apply报名状态失败" );
                }
            }
            DB::commit();

            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }
}
